package com.yswee.mallnavi.view.intro;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.yswee.framework.utils.ViewInject;
import com.yswee.framework.widget.clickedtab.ClickedTabBar;
import com.yswee.mallnavi.R;
import com.yswee.mallnavi.context.ContextConstant;
import com.yswee.mallnavi.entity.info.InfoEntity;

import java.util.ArrayList;


/***
 * TabBar
 */
public class TabBar extends ClickedTabBar {

    @ViewInject
    private ImageView imgnavi01;
    @ViewInject
    private ImageView imgnavi02;
    @ViewInject
    private ImageView imgnavi03;
    @ViewInject
    private ImageView imgnavi04;
    @ViewInject
    private ImageView imgnavi05;
    @ViewInject
    private ImageView imgtab01;
    @ViewInject
    private ImageView imgtab02;
    @ViewInject
    private ImageView imgtab03;
    @ViewInject
    private ImageView imgtab04;
    @ViewInject
    private ImageView imgtab05;

    public TabBar(Context context) {
        super(context);
    }

    public TabBar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.view_info_intro_tabbar;
    }

    @Override
    protected void onBindListener() {
        super.onBindListener();
    }

    @Override
    protected int[] getChildViewResIDs() {
        return new int[] {
        };
    }

    public void setDataArray(ArrayList<InfoEntity> array) {
        if(array != null && array.size() > 0) {
            int size = Math.min(array.size(), 5);
            childviewList = new View[size];
            for(int i = 0; i < size; i++) {
                if (i == 0) {
                    childviewList[i] = imgtab01;
                } else if (i == 1) {
                    childviewList[i] = imgtab02;
                } else if (i == 2) {
                    childviewList[i] = imgtab03;
                } else if (i == 3) {
                    childviewList[i] = imgtab04;
                } else if (i == 4) {
                    childviewList[i] = imgtab05;
                }
                if (childviewList[i] != null){
                    Glide.with(this)
                        .load(array.get(i).pictureUrl)
                        .into((ImageView) childviewList[i]);
                    final int index = i;
                    childviewList[i].setOnClickListener(new OnClickListener() {
                        public void onClick(View v) {
                            if(null != listener) {
                                listener.onTabChanged(index);
                            }
                            setTabIndex(index);
                        }
                    });
                }
            }
            for (int i = size; i < 5; i++) {
                View view = null;
                if (i == 0) {
                    view = imgtab01;
                } else if (i == 1) {
                    view = imgtab02;
                } else if (i == 2) {
                    view = imgtab03;
                } else if (i == 3) {
                    view = imgtab04;
                } else if (i == 4) {
                    view = imgtab05;
                }
                view.setVisibility(View.INVISIBLE);
            }
        }
    }

    @SuppressWarnings("deprecation")
    @Override
    protected void focusChildView(int index, View v) {
        ImageView vwIcon = null;
        View vwNavi = null;
        if (index == 0) {
            vwIcon = imgtab01;
            vwNavi = imgnavi01;
        } else if (index == 1) {
            vwIcon = imgtab02;
            vwNavi = imgnavi02;
        } else if (index == 2) {
            vwIcon = imgtab03;
            vwNavi = imgnavi03;
        } else if (index == 3) {
            vwIcon = imgtab04;
            vwNavi = imgnavi04;
        } else if (index == 4) {
            vwIcon = imgtab05;
            vwNavi = imgnavi05;
        }
        vwNavi.setVisibility(View.VISIBLE);
        if (index == 0) {
            vwIcon.setBackgroundResource(ContextConstant.IS_LANDSCAPE ?
                    R.drawable.intro_tabbar_h_l_sel_bg : R.drawable.intro_tabbar_l_sel_bg);
        } else if (index == 4) {
            vwIcon.setBackgroundResource(ContextConstant.IS_LANDSCAPE ?
                    R.drawable.intro_tabbar_h_r_sel_bg : R.drawable.intro_tabbar_r_sel_bg);
        } else {
            vwIcon.setBackgroundResource(R.color.widget_tabbar_focus_bg);
        }
    }

    @SuppressWarnings("deprecation")
    @Override
    protected void unfocusChildView(int index, View v) {
        ImageView vwIcon = null;
        View vwNavi = null;
        if (index == 0) {
            vwIcon = imgtab01;
            vwNavi = imgnavi01;
        } else if (index == 1) {
            vwIcon = imgtab02;
            vwNavi = imgnavi02;
        } else if (index == 2) {
            vwIcon = imgtab03;
            vwNavi = imgnavi03;
        } else if (index == 3) {
            vwIcon = imgtab04;
            vwNavi = imgnavi04;
        } else if (index == 4) {
            vwIcon = imgtab05;
            vwNavi = imgnavi05;
        }
        vwNavi.setVisibility(View.INVISIBLE);
        if (index == 0) {
            vwIcon.setBackgroundResource(ContextConstant.IS_LANDSCAPE ?
                    R.drawable.intro_tabbar_h_l_unsel_bg : R.drawable.intro_tabbar_l_unsel_bg);
        } else if (index == 4) {
            vwIcon.setBackgroundResource(ContextConstant.IS_LANDSCAPE ?
                    R.drawable.intro_tabbar_h_r_unsel_bg : R.drawable.intro_tabbar_r_unsel_bg);
        } else {
            vwIcon.setBackgroundResource(R.color.widget_tabbar_unfocus_bg);
        }
    }

}